package com.lh.music.fragment.localsong;


import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lh.music.R;
import com.lh.music.activity.SongDetailActivity;
import com.lh.music.adapter.AlbumAdapter;
import com.lh.music.datamanager.DataManager;
import com.lh.music.entity.Album;
import com.lh.music.entity.Song;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class AlbumFragment extends Fragment {
    private View view;
    private ListView listView;
    private AlbumAdapter adapter;
    private List<Album> mData=new ArrayList<>();
    public AlbumFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (view==null){
            view=inflater.inflate(R.layout.fragment_album, container, false);
            listView= (ListView) view.findViewById(R.id.listView);
            adapter=new AlbumAdapter(getActivity(),mData);
            listView.setAdapter(adapter);
            new GetSongTask().execute();
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent=new Intent(getActivity(), SongDetailActivity.class);
                    Bundle bundle=new Bundle();
                    ArrayList<Song> l= (ArrayList<Song>) mData.get(position).getSongList();
                    bundle.putSerializable("songList",l);
                    intent.putExtra("bundle",bundle);
                    intent.putExtra("flag","album");
                    startActivity(intent);
                }
            });
        }
        return view;
    }
    class GetSongTask extends AsyncTask<Void,Void,Void>{

        @Override
        protected Void doInBackground(Void... params) {
            DataManager dataManager=new DataManager(getActivity());
            List<Song> songs=dataManager.findAllSongOrderBySinger();
            Album album=null;
            List<Song>  songList=null;
            for (int i=0;i<songs.size();i++){
                if (i==0){
                    album=new Album();
                    songList=new ArrayList<>();
                    album.setArtist(songs.get(i).getArtist());
                    album.setAlbumName(songs.get(i).getAlbum());
                    songList.add(songs.get(i));
                    if (i==songs.size()-1){
                        album.setSongList(songList);
                        mData.add(album);
                    }
                }else if (songs.get(i).getAlbum().equals(songs.get(i-1).getAlbum())){
                    songList.add(songs.get(i));
                    if (i==songs.size()-1){
                        album.setSongList(songList);
                        mData.add(album);
                    }
                }else{
                    album.setSongList(songList);
                    mData.add(album);
                    album=new Album();
                    songList=new ArrayList<>();
                    album.setArtist(songs.get(i).getArtist());
                    album.setAlbumName(songs.get(i).getAlbum());
                    songList.add(songs.get(i));
                    if (i==songs.size()-1){
                        album.setSongList(songList);
                        mData.add(album);
                    }
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            adapter.notifyDataSetChanged();
        }
    }
}
